<template>
  <!-- 提示弹窗 -->
  <uni-popup
    ref="popup"
    :mask-click="false"
  >
    <div class="container">
      <scroll-view
        class="container-scroll"
        scroll-y
        enable-flex
        :show-scrollbar="false"
      >
        <div class="title">
          <text class="title-text">
            活动规则
          </text>
        </div>
        <rich-text :nodes="rule" />
      </scroll-view>
      <div
        class="i-know"
        @click="close"
      >
        <text class="text">
          我知道了
        </text>
      </div>
    </div>
  </uni-popup>
</template>

<script setup lang="ts">
import { reqGetActivityRule } from '@/api/api-activity';
import type { ACTIVITY_RULE_KEY } from '@/static/constant/view-constant';
import { addClassToHtml } from '@/utils/tools';

const popup = ref<UniHelper.UniPopupInstance>();
const rule = ref('');

type Props = {
  ruleKey: (typeof ACTIVITY_RULE_KEY)[keyof typeof ACTIVITY_RULE_KEY];
};

const props = defineProps<Props>();

onMounted(async () => {
  if (!props.ruleKey) return;
  const ruleRes = await reqGetActivityRule(props.ruleKey);
  rule.value = addClassToHtml(ruleRes.data.data.ruleValue);
});

/** 弹窗打开 */
const open = () => {
  console.log('open');
  if (!popup.value?.open) return;
  popup.value?.open();
};

/** 弹窗关闭 */
const close = () => {
  if (!popup.value?.close) return;
  popup.value?.close();
  emit('popupClose');
};

defineExpose({
  open,
  close,
});

const emit = defineEmits<{(event: 'clickRule'): void,
  (event: 'popupClose'): void
}>();

</script>

<style scoped lang="scss">
.container {
    position: relative;

    width: 90vw;
    max-height: 80vh;

    background: #FFFFFF;
    box-shadow: 0px 7 19px 0px rgba(0, 0, 0, 0.3);
    border-radius: 41rpx 41rpx 41rpx 41rpx;

    padding: 22rpx 16rpx 24rpx;

    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;

    .container-bg {
        position: absolute;
        top: 0;
        left: 0;
        z-index: -1;

        width: 100%;
        height: 100%;
    }

    .container-scroll {
        max-height: 60vh;
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
    }

    .title-image {
        position: absolute;
        top: -200rpx;
        z-index: 100;
        width: 572rpx;
        height: 200rpx;
    }

    .title {
        background-image: url("https://ustatic.hudongmiao.com/miao/activity/202505/titleBg.png");
        background-size: 565rpx 90rpx;
        width: 565rpx;
        height: 90rpx;
        display: flex;
        align-items: center;
        justify-content: center;

        .title-text {
            font-weight: 700;
            font-size: 45rpx;
            color: #FFFFFF;
            line-height: 65rpx;
            text-align: justify;
            font-style: normal;
            text-transform: none;
            margin-bottom: 10rpx;
        }
    }

    .intro-text {
        margin-top: 20rpx;
        width: 565rpx;
        font-weight: 400;
        font-size: 26rpx;
        color: #666666;
        line-height: 37rpx;
        text-align: justify;
        font-style: normal;
        text-transform: none;
    }

    .lottery-content {
        width: 565rpx;
        height: 653rpx;
        background-image: url("https://ustatic.hudongmiao.com/miao/activity/202505/contentBg.png");
        background-size: 100% 100%;
        background-repeat: no-repeat;
        margin-top: 20rpx;
        display: flex;
        justify-content: center;
        align-items: center;

        .lottery-items {
            width: 507rpx;
            height: 591rpx;
            background: #FFFBDD;
            border-radius: 15rpx 15rpx 15rpx 15rpx;

            display: grid;
            grid-template-columns: repeat(3, auto);
            grid-template-rows: repeat(3, auto);
            column-gap: 27rpx;
            row-gap: 9rpx;

            .lottery-item {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;

                .image {
                    width: 151rpx;
                    height: 151rpx;
                    background-color: #FFFFFF;
                }

                .text {
                    width: 151rpx;
                    height: 40rpx;
                    font-weight: 400;
                    font-size: 19rpx;
                    color: #9B642E;
                    line-height: 40rpx;
                    text-align: center;
                    font-style: normal;
                    text-transform: none;
                }
            }
        }
    }

    .i-know {
        margin-top: 20rpx;
        width: 319rpx;
        height: 95rpx;
        background-image: url("https://ustatic.hudongmiao.com/miao/activity/202505/yellowButton2.png");
        background-size: 319rpx 95rpx;
        display: flex;
        align-items: center;
        justify-content: center;

        .text {
            font-weight: 500;
            font-size: 34rpx;
            color: #FFFFFF;
            line-height: 49rpx;
            text-align: center;
            font-style: normal;
            text-transform: none;
        }
    }

}
</style>

<style lang="scss">
// Add rich text styles
.rich-h1 {
  font-size: 17px;
  font-weight: bold;
  color: #333;
  padding: 0 10px;
}

.rich-h2 {
    font-size: 15px;
    font-weight: bold;
    color: #333;
    margin-top: 10px;
}
.rich-h2::after {
    content: "：";
    margin-left: 2px;
}

.rich-p {
    font-size: 15px;
    line-height: 28px;
    color: #333;
}
.rich-ol {
  margin-top: 5px;
  padding: 0 0 0 20px;
  font-size: 15px;
  color: #333;
}

.rich-table {
  margin: 5px auto;
  border-collapse: collapse;
  border: 1px solid #333;
  box-sizing: border-box;
  font-size: 11px;
  color: #333;

  .rich-tr {
    border: 1px solid #333;

    .rich-td {
      border: 1px solid #333;
      padding: 5px;
    }
  }
}
</style>
